// BitScanForward.cpp
// compile with: /EHsc
#include <iostream>
#include <intrin.h>
using namespace std;

#pragma intrinsic(_BitScanForward)

void test(unsigned long mask)
{
   unsigned long index;
   unsigned char isNonzero;

   isNonzero = _BitScanForward(&index, mask);
   if (isNonzero)
   {
      cout << "Mask: " << mask << " Index: " << index << endl;
   }
   else
   {
      cout << "No set bits found.  Mask is zero." << endl;
   }
}

int main()
{
   unsigned long mask = 0x3;
   for (int i=0; i<32; ++i)
   {
       test(mask << i);
   }
}
